package com.ai.networking.netty.client;

import com.ai.networking.netty.comm.CommonNetty;
import com.ai.entity.comm.SysNetty;
import com.ai.service.networking.ISysNettyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class ClientInit {

    @Resource
    ISysNettyService nettyService;

    @PostConstruct
    public void init() {
        log.info("================初始化数据========================");

        List<SysNetty> sysNettyList = dataInit();//获取所有需要开启的端口ip

        log.info("=========================== 所有需要进行连接的ip和端口  ===========================");
        for (SysNetty sysNetty : sysNettyList) {
            log.info("===========================  {} ===========================", sysNetty.toString());
        }


        clientInit(sysNettyList);
    }

    private List<SysNetty> dataInit() {
        return nettyService.getNativeStartClientList();
    }

    private void clientInit(List<SysNetty> sysNettyList) {
        for (SysNetty sysNetty : sysNettyList) {
            //将所有的需要进行连接的端口进行一个记录，用于后续的心跳测试与重连操作
            CommonNetty.NATIVE_CLIENT_MAP.put(sysNetty.getCode(), sysNetty);
            log.info("=========================== 客户端准备连接" + sysNetty + "  ===========================");
            //每一个客户端独占一个线程
            new BootNettyClientThread(sysNetty).start();
        }
    }
}
